
#include "stdafx.h"
#include "UdpCallBenchMgr.h"
#include "UdpCallBenchApp.h"
#include "ReloadCode.h"
#include "ReloadPacket.h"
#include "ReloadConfigure.h"
#include "ReloadUdpCallPacket.h"

UdpCallBenchAppConfigure::UdpCallBenchAppConfigure()
{

}

UdpCallBenchAppConfigure::~UdpCallBenchAppConfigure()
{

}

retT UdpCallBenchAppConfigure::init( const std::string _configFileName/*=DEFAULT_CONFIG_FILE_NAME*/ )
{
	RNL(asyframe::ConfigureBase::init(UDPCALLBENCH_CONFIG_FILE_NAME));
	try
	{
		instanceName_ = pt_.get<std::string>("Config.UdpCallBench.instanceName");
		dataThreadSize_=pt_.get<std::size_t>("Config.UdpCallBench.dataThreadSize");
	}
	catch(std::exception *)
	{
		return ERROR_APPBASE_INIT_READ_CONFIG_FAIL;
	}
	RETSUCCESSNL;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

UdpCallBenchApp::UdpCallBenchApp( asyframe::ConfigureBase* _pConfigure /*= new asyframe::ConfigureBase()*/ )
:asyframe::AppBase(_pConfigure),pUdpCallBenchMgr_(),success_(0),fail_(0),timeout_(0),error_(0),actor_(0),timeSpan_(0)
{

}

UdpCallBenchApp::~UdpCallBenchApp()
{
	pUdpCallBenchMgr_.reset();
	success_ = 0;
	fail_ = 0;
	timeout_ = 0;
	error_ = 0;
	actor_ = 0;
	timeSpan_ = 0;
}

retT UdpCallBenchApp::init()
{
	R(asyframe::AppBase::init());
	boost::shared_ptr<boost::asio::ip::udp::endpoint> pRemoteEndpoint(new boost::asio::ip::udp::endpoint(
		boost::asio::ip::address::from_string("127.0.0.1"),ReloadUdpCall_ListeningPort_Store));
	pUdpCallBenchMgr_.reset(new UdpCallBenchMgr(pRemoteEndpoint, ((UdpCallBenchAppConfigure*)asyframe::getApp()->getConfigure())->dataThreadSize_));
	R(addActorMgr(pUdpCallBenchMgr_));
	RETSUCCESS;
}

void UdpCallBenchApp::gatherResult( int _success, int _fail, int _timeout, int _error )
{
	int timeSpan = (boost::posix_time::second_clock::local_time() - startTime_).total_seconds();
	int total = _success + _fail + _timeout + _error;
	
	std::cout << std::fixed << std::setprecision(2);
	std::cout << "actor[" << actor_ << "]: " << "total:" << total << "/" << timeSpan << "s" << std::endl;
	std::cout << "success:" << _success << "(" << ((float)_success/(float)total)*100 << "%)/" << (float)_success/(float)timeSpan;
	std::cout << " fail:" << _fail << "(" << ((float)_fail/(float)total)*100 << "%)/" << (float)_fail/(float)timeSpan;
	std::cout << " timeout:" << _timeout << "(" << ((float)_timeout/(float)total)*100 << "%)/" << (float)_timeout/(float)timeSpan;
	std::cout << " error:" << _error << "(" << ((float)_error/(float)total)*100 << "%)/" << (float)_error/(float)timeSpan;
	std::cout << std::endl;
	std::cout << std::endl;

	++actor_;
	success_ += _success;
	fail_ += _fail;
	timeout_ += _timeout;
	error_ += _error;
}

retT UdpCallBenchApp::start()
{
	R(asyframe::AppBase::start());
	startTime_ = boost::posix_time::second_clock::local_time();
	RETSUCCESS;
}

retT UdpCallBenchApp::close()
{
	R(asyframe::AppBase::close());
	timeSpan_ = (boost::posix_time::second_clock::local_time() - startTime_).total_seconds();
	RETSUCCESS;
}

retT UdpCallBenchApp::exitInstance()
{
	R(asyframe::AppBase::exitInstance());
	int total = success_ + fail_ + timeout_ + error_;

	std::cout << std::fixed << std::setprecision(2);
	std::cout << "total:" << total << "/" << timeSpan_ << "s" << std::endl;
	std::cout << "success:" << success_ << "(" << ((float)success_/(float)total)*100 << "%)/" << (float)success_/(float)timeSpan_;
	std::cout << " fail:" << fail_ << "(" << ((float)fail_/(float)total)*100 << "%)/" << (float)fail_/(float)timeSpan_;
	std::cout << " timeout:" << timeout_ << "(" << ((float)timeout_/(float)total)*100 << "%)/" << (float)timeout_/(float)timeSpan_;
	std::cout << " error:" << error_ << "(" << ((float)error_/(float)total)*100 << "%)/" << (float)error_/(float)timeSpan_;
	std::cout << std::endl;
	std::cout << std::endl;

	RETSUCCESS;
}

//void UdpCallBenchApp::setTestCodeSize( std::size_t _testCodeSize )
//{
//	testCode_.resize(_testCodeSize);
//	for(std::size_t i = 0; i < _testCodeSize; ++i)
//	{
//		testCode_[i].success_ = 0;
//		testCode_[i].fail_ = 0;
//		testCode_[i].timeout_ = 0;
//		testCode_[i].error_ = 0;
//	}
//}
//
//void UdpCallBenchApp::incSuccess( int _index )
//{
//	testCode_[_index].success_++;
//}
//
//void UdpCallBenchApp::incFail( int _index )
//{
//	testCode_[_index].fail_++;
//}
//
//void UdpCallBenchApp::incTimeout( int _index )
//{
//	testCode_[_index].timeout_++;
//}
//
//void UdpCallBenchApp::incError( int _index )
//{
//	testCode_[_index].error_++;
//}
